/*
 * @Author: xiguan
 * @Email: xiguan.teng@qq.com
 * @Version: 1.0
 * @Date: 2022-09-12 10:45:43
 * @LastEditTime: 2022-09-12 15:09:59
 */
#include <bits/stdc++.h>
#include <stdlib.h>

using namespace std;




void printEdge( vector<vector<int>> &matrix, int tR, int tC, int dR, int dC ){
    if( tR == dR ){
        for( int idx = tC; idx <= dC; ++ idx ){
            cout << matrix[tR][idx] << " ";
        }
    }else if( tC == dC ){
        for( int idx = tR; idx <= dR; ++ idx ){
            cout << matrix[idx][tC] << " ";
        }
    }else{
        int curC = tC;
        int curR = tR;
        while( curC != dC ){
            cout << matrix[tR][curC ++] << " ";
        }
        while( curR != dR ){
            cout << matrix[curR ++][dC] << " ";
        }
        while( curC != tC ){
            cout << matrix[dR][curC --] << " ";
        }
        while( curR != tR ){
            cout << matrix[curR --][tC] << " ";
        }
    }
}



void spiralOrderPrint( vector<vector<int>> &matrix ){
    int tR = 0;
    int tC = 0;
    int dR = matrix.size() - 1;
    int dC = matrix[0].size() - 1;

    while( tR <= dR && tC <= dC ){
        printEdge( matrix, tR ++, tC ++, dR --, dC -- );
    }
    cout << endl;
}


void rotateEdge( vector<vector<int>> &matrix, int tR, int tC, int dR, int dC ){
    int times = dC - tC;
    int tmp = 0;

    for( int idx = 0; idx != times; idx ++ ){
        tmp = matrix[tR][tC + idx];
        matrix[tR][tC + idx] = matrix[dR - idx][tC];
        matrix[dR - idx][tC] = matrix[dR][dC - idx];
        matrix[dR][dC - idx] = matrix[tR + idx][dC];
        matrix[tR + idx][dC] = tmp;
    }
}



void rotate( vector<vector<int>> &matrix ){
    int tR = 0;
    int tC = 0;
    int dR = matrix.size() - 1;
    int dC = matrix[0].size() - 1;

    while( tR < dR ){
        rotateEdge( matrix, tR ++, tC ++, dR --, dC -- );
    }
}


void printLevel( vector<vector<int>> &matrix, int tR, int tC, int dR, int dC, bool f ){
    if( f ){
        while( tR != dR + 1 ){
            cout << matrix[tR ++][tC --] << " ";
        }
    }else{
        while( dR != tR - 1 ){
            cout << matrix[dR --][dC ++] << " ";
        }
    }
}


void printMatrixZigZag( vector<vector<int>> &matrix ){
    int tR = 0;
    int tC = 0;
    int dR = 0;
    int dC = 0;
    int endR = matrix.size() - 1;
    int endC = matrix[0].size() - 1;
    bool fromUp = false;

    while( tR != endR + 1 ){
        printLevel( matrix, tR, tC, dR, dC, fromUp );
        tR = tC == endC ? tR + 1 : tR;
        tC = tC == endC ? tC : tC + 1;
        dC = dR == endR ? dC + 1 : dC;
        dR = dR == endR ? dR : dR + 1;
        fromUp = !fromUp;
    }
    cout << endl;
}



int main( int argc, char *argv[] ){

    int n, m; cin >> n >> m;
    vector<vector<int>> arr;
    for( int idx = 0; idx < n; ++ idx ){
        vector<int> v;
        for( int jdx = 0; jdx < m; ++ jdx ){
            int t; cin >> t;
            v.push_back( t );
        }
        arr.push_back( v );
    }
    // spiralOrderPrint( arr );
    // rotate( arr );
    printMatrixZigZag( arr );
    // for( int idx = 0; idx < n; ++ idx ){
    //     for( int jdx = 0; jdx < n; ++ jdx ){
    //         cout << arr[idx][jdx] << " ";
    //     }
    //     cout << endl;
    // }

    return 0;
}